package com.xinjian.jianba.util.rongyungroup;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.xinjian.jianba.controller.AbstractController;
import com.xinjian.jianba.dto.GroupDto;
import com.xinjian.jianba.exception.BusinessException;
import com.xinjian.jianba.exception.ResponseCode;

import io.rong.RongCloud;
import io.rong.methods.group.Group;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.group.UserGroup;
import io.rong.models.response.GroupUserQueryResult;

/**
 *
 * 群组服务示例
 * @author RongCloud
 *
 * @version 3.0.0
 */
@Component
public class GroupUtil {
	private static final Logger log = LoggerFactory
			.getLogger(GroupUtil.class);
	private static  String appKey;
    /**
     * 融云appSecret
     */
    private static  String appSecret;
    @Value("${RongYun.appKey}")
	public void setAppKey(String appKey) {
    	GroupUtil.appKey = appKey;
	}
    @Value("${RongYun.appSecret}")
	public  void setAppSecret(String appSecret) {
    	GroupUtil.appSecret = appSecret;
	}
	/**
	 * 自定义api地址
	 * */
	private static final String api = "http://api.cn.ronghub.com";

	/**
	 * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#join
	 *
	 * 邀请用户加入群组
	 * @throws Exception 
	 *
	 */
	public static Result inviteGroup(String[] infoIds,String groupId,String groupName){
		RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
		//自定义 api 地址方式
		// RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret,api);

		Group Group = rongCloud.group;
		GroupMember[] members = infoIdsToGroupMember(infoIds);
		GroupModel group = new GroupModel()
				.setId(groupId)
				.setMembers(members)
				.setName(groupName);
		Result groupInviteResult;
		try {
			groupInviteResult = (Result)Group.invite(group);
		} catch (Exception e) {
			log.info("invite:失败{}",e);
			throw new BusinessException(ResponseCode.ERROR,e.getMessage());
		}
		if(groupInviteResult.code.equals(200)){
			log.info("invite::成功 {}",groupInviteResult.toString());
		}else{
			log.info("invite::状态码失败 {}",groupInviteResult.toString());
			throw new BusinessException(groupInviteResult.getCode(), groupInviteResult.getMsg());
		}
		return groupInviteResult;
	}
	
	/**
	 * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#join
	 *
	 * 用户加入指定群组
	 * @throws Exception 
	 *
	 */
	public static Result joinGroup(String[] infoIds,String groupId,String groupName){
		RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
		//自定义 api 地址方式
		// RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret,api);
		GroupMember[] members = infoIdsToGroupMember(infoIds);
		Group Group = rongCloud.group;

		GroupModel group = new GroupModel()
				.setId(groupId)
				.setMembers(members)
				.setName(groupName);
		Result groupJoinResult;
		try {
			groupJoinResult = (Result)Group.join(group);
		} catch (Exception e) {
			log.info("join:失败{}",e);
			throw new BusinessException(ResponseCode.ERROR,e.getMessage());
		}
		if(groupJoinResult.code.equals(200)){
			log.info("join::成功 {}",groupJoinResult.toString());
		}else{
			log.info("join::状态码失败 {}",groupJoinResult.toString());
			throw new BusinessException(groupJoinResult.getCode(), groupJoinResult.getMsg());
		}
		return groupJoinResult;
	}
	/**
	 * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#getMembers
	 *
	 * 查询群成员方法
	 * @throws Exception 
	 *
	 */
	public static Result getGroupInfos(String groupId){
		RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
		//自定义 api 地址方式
		// RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret,api);

		Group Group = rongCloud.group;
		GroupModel group = new GroupModel().setId(groupId);
		GroupUserQueryResult getMemberesult;
		try {
			getMemberesult = Group.get(group);
		} catch (Exception e) {
			log.info("group getMember:失败{}",e);
			throw new BusinessException(ResponseCode.ERROR,e.getMessage());
		}
		if(getMemberesult.code.equals(200)){
			log.info("group getMember::成功 {}",getMemberesult.toString());
		}else{
			log.info("group getMember::状态码失败 {}",getMemberesult.toString());
			throw new BusinessException(getMemberesult.getCode(), getMemberesult.getMsg());
		}
		return getMemberesult;
	}
	/**
	 * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/group/group.html#quit
	 *
	 * 退出群组
	 * @throws Exception 
	 *
	 */
	public static Result quitGroup(String[] infoIds,String groupId,String groupName){
		RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
		//自定义 api 地址方式
		// RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret,api);

		Group Group = rongCloud.group;
		GroupMember[] members = infoIdsToGroupMember(infoIds);
		GroupModel group = new GroupModel()
				.setId(groupId)
				.setMembers(members)
				.setName(groupName);
		Result groupQuitResult;
		try {
			groupQuitResult = (Result)Group.quit(group);
		} catch (Exception e) {
			log.info("quit:失败{}",e);
			throw new BusinessException(ResponseCode.ERROR,e.getMessage());
		}
		if(groupQuitResult.code.equals(200)){
			log.info("quit::成功 {}",groupQuitResult.toString());
		}else{
			log.info("quit::状态码失败 {}",groupQuitResult.toString());
			throw new BusinessException(groupQuitResult.getCode(), groupQuitResult.getMsg());
		}
		return groupQuitResult;
	}
	public static GroupModel[] groupDtosToGroupModel(GroupDto[] groupDtos) {
		if(groupDtos==null||groupDtos.length==0){
			return null;
		}
		int size=groupDtos.length;
		GroupModel[] groups=new GroupModel[size];
		for(int i=0;i<size;i++){
			groups[i]=new GroupModel().setId(groupDtos[i].getGroupId()).setName(groupDtos[i].getGroupName());
		}
		return groups;
	}
	public static GroupMember[] infoIdsToGroupMember(String[] infoId) {
		if(infoId==null||infoId.length==0){
			return null;
		}
		int size=infoId.length;
		GroupMember[] members=new GroupMember[size];
		for(int i=0;i<size;i++){
			members[i]=new GroupMember().setId(infoId[i]);
		}
		return members;
	}
	//检验群成员id
	public static void checkInfoids(String[] infoIds){
		for(String infoId:infoIds){
			if(infoId.lastIndexOf("a")<0&&infoId.lastIndexOf("b")<0){
				throw new BusinessException(ResponseCode.ERROR,"传入的群成员id中有不合法id");
			}
		}
	}
	public static void checkInfoid(String infoId){
		if(infoId.lastIndexOf("a")<0&&infoId.lastIndexOf("b")<0){
			throw new BusinessException(ResponseCode.ERROR,"传入的群成员id中有不合法id");
		}
	}
	
}